/*
* $Id: HousingForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.common.FormHelper;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.TypeStore;
import apiary.db.managers.DBManager;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.microedition.lcdui.Command;

public class HousingForm extends WizardForm implements ItemEventListener,
                                                       ItemStateListener
{
    public class HousingValue
    {
        public String nameHousing;
        public int idFamily;
        public int typeHousing;
        public int typeFrames;
        public boolean isDelete;

        public HousingValue() {
            nameHousing = new String();
            isDelete = false;
        }
    }

    private Command chooseCommand;
    private CategoryItem curCategory;

    public HousingForm(String name, Integer id, Wizard wiz, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
        Apiary apiary = Apiary.getInstance();
        append(new TextField(apiary.getLocaleString("NameHousing"), "", 256, TextField.ANY));
        append(apiary.getLocaleString("NameFamily"));
        String nameEmpty = Apiary.getInstance().getLocaleString("NoFamily");
        CategoryItem item = FormHelper.createCategoryItem(this, "family", nameEmpty, "");
        item.setState("0");
        append(item);
        append(apiary.getLocaleString("TypeHousing"));
        item = FormHelper.createCategoryItem(this, "typeH", "", "");
        Hashtable types = DBManager.getInstance().getTypesHousing();
        item.setName((String) types.get(new Integer(TypeStore.TYPEH_8)));
        item.setState(String.valueOf(TypeStore.TYPEH_8));
        append(item);
        append(apiary.getLocaleString("TypeFrame"));
        item = FormHelper.createCategoryItem(this, "typeF", "", "");
        types = DBManager.getInstance().getTypesFrame();
        item.setName((String) types.get(new Integer(TypeStore.TYPEF_300)));
        item.setState(String.valueOf(TypeStore.TYPEF_300));
        append(item);

        setItemStateListener(this);
    }

    public Object GetDataForm() {
        HousingValue housing = new HousingValue();
        TextField text = (TextField) get(0);
        housing.nameHousing = text.getString();
        CategoryItem item = (CategoryItem) get(2);
        housing.idFamily = Integer.parseInt(item.getState());
        item = (CategoryItem) get(4);
        housing.typeHousing = Integer.parseInt(item.getState());
        item = (CategoryItem) get(6);
        housing.typeFrames = Integer.parseInt(item.getState());
        return housing;
    }

    protected void commandUser(Command command) {
        itemNotifyEvent(curCategory, PRESS);
    }

    public void setHousingValue(HousingValue housing) {
        DBManager dBManager = DBManager.getInstance();
        TextField text = (TextField) get(0);
        text.setString(housing.nameHousing);
        CategoryItem item = (CategoryItem) get(2);
        FamilyRecord family = dBManager.getFamily(housing.idFamily);
        if(family == null) {
            String nameEmpty = Apiary.getInstance().getLocaleString("NoFamily");
            item.setName(nameEmpty);
        }  else
            item.setName(family.name);
        item.setState(String.valueOf(housing.idFamily));
        item = (CategoryItem) get(4);
        Hashtable types = dBManager.getTypesHousing();
        item.setName((String) types.get(new Integer(housing.typeHousing)));
        item.setState(String.valueOf(housing.typeHousing));
        item = (CategoryItem) get(6);
        types = dBManager.getTypesFrame();
        item.setName((String) types.get(new Integer(housing.typeFrames)));
        item.setState(String.valueOf(housing.typeFrames));
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(PRESS)){
            Apiary apiary = Apiary.getInstance();
            String name = "";
            CategoryItem categoryItem = (CategoryItem) item;
            if(categoryItem.getId().equals("family"))
                name = apiary.getLocaleString("ChooseFamily");
            if(categoryItem.getId().equals("typeH"))
                name = apiary.getLocaleString("TypeHousing");
            else if(categoryItem.getId().equals("typeF"))
                name = apiary.getLocaleString("TypeFrame");
            ChooseList list = new ChooseList(name, item);
            if(categoryItem.getId().equals("family"))
                FormHelper.fillFamilyList(list);
            if(categoryItem.getId().equals("typeH"))
                fillTypeHList(list);
            else if(categoryItem.getId().equals("typeF"))
                fillTypeFList(list);
            list.setBackScreen(this);
            apiary.Display(list);
        }
    }

    public void itemStateChanged(Item item) {
        if(item.getClass() != CategoryItem.class)
            return;
        CategoryItem categoryItem = (CategoryItem) item;
        Apiary apiary = Apiary.getInstance();
        if(categoryItem.getActive()) {
            chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.BACK, 0);
            addCommand(chooseCommand);
            curCategory = categoryItem;
        } else {
            removeCommand(chooseCommand);
            curCategory = null;
        }
    }

    private void fillTypeHList(ChooseList list) {
        Hashtable types = DBManager.getInstance().getTypesHousing();
        Enumeration typesEnum = types.keys();
        while(typesEnum.hasMoreElements()) {
            Integer type = (Integer) typesEnum.nextElement();
            String typeName = (String) types.get(type);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(type), typeName, "");
            list.insert(0, item);
        }
    }

    private void fillTypeFList(ChooseList list) {
        Hashtable types = DBManager.getInstance().getTypesFrame();
        Enumeration typesEnum = types.keys();
        while(typesEnum.hasMoreElements()) {
            Integer type = (Integer) typesEnum.nextElement();
            String typeName = (String) types.get(type);
            CategoryItem item = FormHelper.createCategoryItem(list,
                    String.valueOf(type), typeName, "");
            list.insert(0, item);
        }
    }
}
